package cn.zjj.org.service.impl;

import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.basic.util.Md5Utils;
import cn.zjj.basic.util.PageList;
import cn.zjj.basic.util.StrUtils;
import cn.zjj.org.domain.Employee;
import cn.zjj.org.mapper.EmployeeMapper;
import cn.zjj.org.query.EmployeeQuery;
import cn.zjj.org.service.IEmployeeService;
import cn.zjj.user.domain.Logininfo;
import cn.zjj.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void insert(Employee employee) {

        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);

        //赋值到字段
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);


        Logininfo logininfo = setEmpLogininfoMsg(employee);//添加logininfo并返回id
        logininfoMapper.insert(logininfo);

        employee.setLogininfo_id(logininfo.getId());//把返回的id赋值给emp中info字段

        employeeMapper.insert(employee);//添加




        //domain里定义了role_id可以接收到前端的参数   添加中间表
        employeeMapper.insertEmployeeRole(employee.getId(),employee.getRole_id());

    }

    @Override
    public void update(Employee employee) {
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);

        //赋值到字段
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);



        //给新logininfo赋值  //logininfo是没有id的,因为替换相同字段,那么logininfo的id字段和emp的id字段替换,明显错误,需要重新赋值
        Long logininfo_id = employeeMapper.selectById(employee.getId()).getLogininfo_id();

        Logininfo logininfo = setEmpLogininfoMsg(employee);
        //把id赋值给logininfo中id字段
        logininfo.setId(logininfo_id);
        //修改logininfo
        logininfoMapper.update(logininfo);

        //修改员工
        employeeMapper.update(employee);

        // 根据id删除t_employee_role
        employeeMapper.deleteEmployeeRole(employee.getId());

        // 再加一波
        employeeMapper.insertEmployeeRole(employee.getId(),employee.getRole_id());


    }
    private Logininfo setEmpLogininfoMsg(Employee employee){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//默认管理员
        return logininfo;
    }


    @Override
    public void delete(Long id) {
        employeeMapper.deleteEmployeeRole(id);

        employeeMapper.delete(id);


    }

    @Override
    public List<Employee> selectByShopId(Long shopid) {
        return employeeMapper.selectByShopId(shopid);
    }
}
